<template>
  <div class="app-container">
    <el-card class="box-card">
      <div slot="header">
        <span>添加站点</span>
      </div>
      <el-form :inline="true" :model="form">
        <el-form-item label="站点名称">
          <el-input v-model="form.name" placeholder="站点名称" />
        </el-form-item>
        <el-form-item label="站点序列号">
          <el-input v-model="form.vin" placeholder="vin" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="saveVin">保存</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <hr />
    <el-card class="box-card">
      <div slot="header">
        <span>站点操作</span>
      </div>
      站点选择
      <el-select v-model="id" placeholder="请选择" @change="changeVin">
        <el-option
          v-for="item in vinList"
          :key="item.id"
          :label="item.vin"
          :value="item.id">
        </el-option>
      </el-select>
      <el-form :inline="true" :model="formData">
        <el-form-item label="站点名称"> {{ formData.name }} </el-form-item>
        <el-form-item label="运行状态"> {{ formData.runStatus }} </el-form-item>
        <el-form-item label="运行模式"> {{ formData.runModel }} </el-form-item>
        <el-form-item label="在线状态"> {{ formData.online }} </el-form-item>
        <el-form-item label="报警状态"> {{ formData.alarm }} </el-form-item>
      </el-form>
      <el-form :inline="true" :model="formVersion">
        <el-button type="primary" @click="queryVersion">版本查询</el-button>
        <el-form-item label="当前版本"> {{ formVersion.version }} </el-form-item>
      </el-form>
      <el-form :inline="true" :model="updateForm">
        <table>
          <tr>
            <td>
              <el-form-item label="URL">
                <el-input v-model="updateForm.url" placeholder="url"></el-input>
              </el-form-item>
            </td>
            <td>
              <el-form-item label="接入点">
                <el-input v-model="updateForm.dialPoint" placeholder="接入点"></el-input>
              </el-form-item>
            </td>
          </tr>
          <tr>
            <td>
              <el-form-item label="IP">
                <el-input v-model="updateForm.ip" placeholder="IP"></el-input>
              </el-form-item>
            </td>
            <td>
              <el-form-item label="PORT">
                <el-input v-model="updateForm.port" placeholder="PORT"></el-input>
              </el-form-item>
            </td>
          </tr>
          <tr>
            <td>
              <el-form-item label="USER">
                <el-input v-model="updateForm.user" placeholder="USER"></el-input>
              </el-form-item>
            </td>
            <td>
              <el-form-item label="PASSWD">
                <el-input v-model="updateForm.passwd" placeholder="PASSWD"></el-input>
              </el-form-item>
            </td>
          </tr>
          <tr>
            <td>
              <el-form-item label="硬件版本">
                <el-input v-model="updateForm.hardwareVersion" placeholder="硬件版本"></el-input>
              </el-form-item>
            </td>
            <td>
              <el-form-item label="软件版本">
                <el-input v-model="updateForm.softwareVersion" placeholder="软件版本"></el-input>
              </el-form-item>
            </td>
          </tr>
          <tr>
            <td>
              <el-form-item label="制造商代码">
                <el-input v-model="updateForm.manufacturer" placeholder="制造商代码"></el-input>
              </el-form-item>
            </td>
            <td>
              <el-form-item label="升级超时">
                <el-input v-model="updateForm.timeLimit" placeholder="升级超时"></el-input>
              </el-form-item>
            </td>
          </tr>
          <tr>
            <td>
              <el-form-item>
                <el-button type="primary" @click="update">升级发送</el-button>
              </el-form-item>
            </td>
          </tr>
        </table>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import { save, getById, queryVersion, query, updateTerminal } from '@/api/battery/data'

export default {
  data() {
    return {
      id: '',
      vin: '',
      vinList: [],
      form: {
        name: '',
        vin: ''
      },
      formData: {
        name: '',
        runStatus: '',
        runModel: '',
        online: '',
        alarm: ''
      },
      formVersion: {
        version: ''
      },
      updateForm: {
        vin: '',
        url: '',
        dialPoint: '',
        ip: '',
        port: '',
        user: '',
        passwd: '',
        hardwareVersion: '',
        softwareVersion: '',
        manufacturer: '',
        timeLimit: ''
      },
      listQuery: {},
      loading: false
    }
  },
  created() {
    this.getList()
  },
  mounted() {
    // this.setMychart()
  },
  methods: {
    saveVin: function() {
      save(this.form).then((res) => {
        if (res.data.code === 200) {
          this.$notify({
            title: '操作成功',
            type: 'success',
            duration: 2500
          })
          this.form.name = ''
          this.form.vin = ''
          this.getList()
        } else {
          this.$notify({
            title: res.data.msg,
            type: 'error',
            duration: 2500
          })
        }
      })
    },
    update: function() {
      updateTerminal(this.id, this.updateForm).then((res) => {
        if (res.data.code === 200) {
          this.$notify({
            title: '操作成功',
            type: 'success',
            duration: 2500
          })
        } else {
          this.$notify({
            title: res.data.msg,
            type: 'error',
            duration: 2500
          })
        }
      })
    },
    queryVersion: function() {
      queryVersion(this.id).then((res) => {
        if (res.data.code === 200) {
          this.formVersion.version = res.data.data
        } else {
          this.$notify({
            title: res.data.msg,
            type: 'error',
            duration: 2500
          })
        }
      })
    },
    getList: function() {
      this.loading = true
      query(this.listQuery).then(response => {
        this.vinList = response.data.data.records
      })
    },
    changeVin: function() {
      getById(this.id).then(response => {
        this.formData = response.data.data
      })
    }
  }
}
</script>

<style>
</style>
